// package/pages/areaCode/areaCodeList/areaCodeList.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    equipment_control_id: '', //设备id
    areaCode: '', //园区区域码
    page: 1, //当前页数
    pageSize: 20,
    totil: 1, //总页数
    areacodeList: [],
    noMore: false,
    loading: false,
    is_load: false, //是否加载
    modal: false,
    modalContent: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.isFranchiser = options.is_franchiser || '';
    this.isRepair = options.is_repair || '';
    var that = this;
    that.setData({
      equipment_control_id: options.id,
      areacodeList: [],
      page: 1,
      noMore: false,
      loading: false,
      is_load: false,
    })
    wx.showLoading({
      title: '加载中'
    });
    that.getVillageAreaCode(); //获取区域码
    that.getEquipmentAreaCode();
  },



  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 1,
    })
    that.getEquipmentAreaCode();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page = that.data.page + 1;
    if (page > that.data.totil) {
      that.setData({
        noMore: true
      })
      return false;
    }
    that.setData({
      page: page
    })
    that.getEquipmentAreaCode();
  },

  //获取下发的区域码列表
  getEquipmentAreaCode() {
    var that = this;
    app.ajax({
      url: 'Common/Equipment/getEquipmentAreaCode',
      data: {
        page: that.data.page,
        pageSize: that.data.pageSize,
        equipment_control_id: that.data.equipment_control_id,
      },
      success: function (res) {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code == 1000) {
          let areacodeList = [];
          if (that.data.page == 1) {
            areacodeList = res.data.data.list;
          } else {
            areacodeList = that.data.areacodeList.concat(res.data.data.list);
          }
          let totil = Math.ceil(res.data.data.count / that.data.pageSize);
          let len = res.data.data.list.length;
          if (that.data.pageSize > len) {
            that.setData({
              noMore: true
            })
          } else {
            that.setData({
              noMore: false
            })
          }
          that.setData({
            areacodeList: areacodeList,
            totil: totil,
            is_load: true
          })
        } else {
          if (that.data.page == 1) {
            that.setData({
              noMore: true,
              is_load: true
            })
          }
        }
      }
    })
  },
  //获取区域码
  getVillageAreaCode() {
    var that = this;
    app.ajax({
      url: 'Common/Equipment/getVillageAreaCode',
      data: {
        equipment_control_id: that.data.equipment_control_id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            areaCode: res.data.data.area_code
          })
        }
      }
    });
  },

  navMiddleAreaCode() {
    let url = "/pages/partner/middle/areaCode/index?id=" + this.data.equipment_control_id
    if (this.isFranchiser) {
      url += "&is_franchiser=1";
    }
    if (this.isRepair) {
      url += "&is_repair=1";
    }
    let _self = this;
    wx.navigateTo({
      url,
      events: {
        acceptDataFromMiddleAreaCode: function (data) {
          if (data) {
            _self.setModal(data);
          }
        }
      }
    })
  },

  setModal(data) {
    this.area_code_id = data.id;
    this.setData({
      modal: true,
      modalContent: `是否下发区域码:` + data.area_code
    })
  },


  handleClick(e) {
    let that = this;
    if (e.detail.index == 1) {
      wx.showLoading({
        mask: true,
      })
      app.ajax({
        url: 'Common/Equipment/setAreaCode',
        data: {
          equipment_control_id: that.data.equipment_control_id,
          area_code_id: that.area_code_id
        },
        success: function (res) {
          wx.hideLoading();
          if (res.data.code == 1000) {
            app.showToast('区域码下发成功', "none", 2000, function () {});
            //成功后更新列表，且在当前页和当前每页条数
            that.getEquipmentAreaCode();
            that.setData({
              modal: false
            })
            delete(that.area_code_id)
          } else {
            app.showToast(res.data.message, "none", 2000, function () {});
          }
        },
        error: e => {
          wx.hideLoading();
          app.showToast("网络错误");
        }
      });
    } else {
      this.setData({
        modal: false
      })
      delete(this.area_code_id)
    }
  },
  handleHidden() {
    this.setData({
      modal: false
    })
  },

  //下发区域码
  sureIssue(data) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要下发区域码：' + data.name,
      confirmText: '确认',
      cancelText: '取消',
      success: function (res) {
        if (res.confirm) {
          app.ajax({
            url: 'Common/Equipment/setAreaCode',
            data: {
              equipment_control_id: that.data.equipment_control_id,
              area_code_id: data.id
            },
            success: function (res) {
              if (res.data.code == 1000) {
                app.showToast('区域码下发成功', "none", 2000, function () {});
                //成功后更新列表，且在当前页和当前每页条数
                that.getEquipmentAreaCode();
              } else {
                app.showToast(res.data.message, "none", 2000, function () {});
              }
            }
          });
        }
      },
    })
  },
  //跳转兼容区域码下发
  sureCompatibleIssue() {
    wx.navigateTo({
      url: '../setAreaCode/setAreaCode?id=' + this.data.equipment_control_id,
    })
  },
})